package com.cbs.android.module.user.user.module;

import com.cbs.android.component.applicationutils.Global;
import com.cbs.android.component.applicationutils.network.ErrorResponse;
import com.cbs.android.component.network.Request;
import com.cbs.android.component.network.Response;
import com.cbs.android.component.network.ResponseHandler;
import com.cbs.android.component.network.decoder.GsonDecoder;
import com.cbs.android.module.user.network.CBSUserRequest;
import com.cbs.android.module.user.user.UserModule;
import com.cbs.android.module.user.user.entity.User;
import com.cbs.android.module.user.user.handler.ModuleHandler;
import com.google.gson.Gson;

import java.net.HttpURLConnection;

import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * Created by broche on 11/3/15.
 */
public class UserModuleSetInformation {
    private static final String UrlTpl = "%s/user/api/v1/users/{uid}/information";
    private User user;
    private ModuleHandler<User> moduleHandler;

    public UserModuleSetInformation(User user, ModuleHandler<User> moduleHandler) {
        this.user = user;
        this.moduleHandler = moduleHandler;
    }

    public void start() {
        if (UserModule.getUser() == null) {
            if (moduleHandler != null) {
                ErrorResponse errorResponse = new ErrorResponse();
                errorResponse.setErrorNo(99999);
                errorResponse.setErrorMsg("请先登录您的账号");
                moduleHandler.onFailure(999, new Gson().toJson(errorResponse));
            }
            return;
        }
        FormBody.Builder builder = new FormBody.Builder();
        if (this.user.getNickName() != null && !this.user.getNickName().equals("")) {
            builder.add("nickname", this.user.getNickName());
        }
        if (this.user.getRealName() != null && !this.user.getRealName().equals("")) {
            builder.add("realname", this.user.getRealName());
        }
        if (this.user.getSex() != -1) {
            builder.add("sex", this.user.getSex() + "");
        }
        if (this.user.getBirthday() != null) {
            builder.add("birthday", this.user.getBirthday());
        }
        if (this.user.getAvatar() != null && !this.user.getAvatar().equals("")) {
            builder.add("avatar", this.user.getAvatar());
        }
        if (this.user.getHomeImage() != null && !this.user.getHomeImage().equals("")) {
            builder.add("homeimage", this.user.getHomeImage());
        }
        if (this.user.getExtra_1() != null) {
            builder.add("extra_1", this.user.getExtra_1());
        }
        if (this.user.getExtra_2() != null) {
            builder.add("extra_2", this.user.getExtra_2());
        }
        if (this.user.getExtra_3() != null) {
            builder.add("extra_3", this.user.getExtra_3());
        }
        if (this.user.getExtra_4() != null) {
            builder.add("extra_4", this.user.getExtra_4());
        }
        if (this.user.getExtra_5() != null) {
            builder.add("extra_5", this.user.getExtra_5());
        }
        RequestBody requestBody = builder.build();
        Request request = new CBSUserRequest(String.format(UrlTpl, Global.getServerHost()))
                .setDecoder(new GsonDecoder(User.class))
                .patch(requestBody)
                .setResponseHandler(new ResponseHandler() {
                    @Override
                    public void onResponse(Response response) {
                        if (response.getCode() == HttpURLConnection.HTTP_CREATED) { // success
                            User user = ((User) response.getResult());
                            // TODO is this really needed?
                            if (UserModule.getUser() != null) {
                                user.setToken(UserModule.getUser().getToken());
                            }
                            UserModule.setUser(user);
                            if (moduleHandler != null) {
                                moduleHandler.onSuccess(user);
                            }
                        } else { // unknown error
                            if (moduleHandler != null) {
                                moduleHandler.onFailure(response.getCode(), response.getResult().toString());
                            }
                        }
                    }

                    @Override
                    public void onException(Request request, Exception e) {
                        if (moduleHandler != null) {
                            moduleHandler.onException(e);
                        }
                    }
                });
        UserModule.getHttpClient().send(request);
    }
}
